<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * Class CreateStaffDormAdjustSniffersTable.
 */
class CreateStaffDormAdjustSniffersTable extends Migration
{
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('staff_dorm_adjust_sniffers', function(Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('staff_id');
            $table->boolean('handled_status')->default(false);
            $table->string('adjust_type')->comment('调整类型:insert,update,delete');
            $table->timestamps();
		});
		$sql=<<<'doc'
DROP TRIGGER IF EXISTS `after_adjust_insert`;
CREATE TRIGGER `after_adjust_insert` AFTER INSERT ON `staff_dorm_adjusts`
FOR EACH ROW insert into staff_dorm_adjust_sniffers(staff_id,adjust_type,created_at,updated_at) values(NEW.staff_id,'insert',now(),now());
DROP TRIGGER IF EXISTS `after_adjust_update`;
CREATE TRIGGER `after_adjust_update` AFTER UPDATE ON `staff_dorm_adjusts`
FOR EACH ROW insert into staff_dorm_adjust_sniffers(staff_id,adjust_type,created_at,updated_at) values(NEW.staff_id,'update',now(),now());
DROP TRIGGER IF EXISTS `after_adjust_delete`;
CREATE TRIGGER `after_adjust_delete` AFTER DELETE ON `staff_dorm_adjusts`
FOR EACH ROW insert into staff_dorm_adjust_sniffers(staff_id,adjust_type,created_at,updated_at) values(OLD.staff_id,'delete',now(),now());
doc;
		DB::unprepared($sql);
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('staff_dorm_adjust_sniffers');
	}
}
